#!/bin/sh
# The following functions are taken from winetricks written by Dan Kegel. 
WINE=${WINE:-wine}
WINETRICKS_CACHE=$HOME/winetrickscache
test -d $WINETRICKS_CACHE || WINETRICKS_CACHE=$HOME/.winetrickscache
WINEPREFIX=${WINEPREFIX:-$HOME/.wine}

mkdir -p "$WINETRICKS_CACHE"

if [ ! -x "`which "wget"`" ]
then
    echo "wget not found. Needed to download files."
    exit 1
fi

if [ ! -x "`which "sha1sum"`" ]
then
    echo "sha1sum not found. Needed to checksum files."
    exit 1
fi

# Download a file
download() {
    file=`basename $1`
    test -f $WINETRICKS_CACHE/$file || (cd $WINETRICKS_CACHE; wget $1)
    if [ "$2"x != ""x ]
    then
    echo "$2  $WINETRICKS_CACHE/$file" > $WINETRICKS_CACHE/$file.sha1sum
    try sha1sum --status -c $WINETRICKS_CACHE/$file.sha1sum
    fi
}

# Execute with error checking
try() {
    # "VAR=foo try cmd" fails to put VAR in the environment
    # with some versions of bash if try is a shell function?!
    # Adding this explicit export works around it.
    export WINEDLLOVERRIDES
    echo Executing "$@"
    "$@"
    status=$?
    if test $status -ne 0
    then
        echo "Note: command '$@' returned status $status.  Aborting."
    exit $status
    fi
}

# Start with a clean install:
rm -rf $WINEPREFIX

#Unattended AHK installation
download "http://www.autohotkey.com/download/AutoHotkey104706_Install.exe" 3d3d8845473dea477d6983d063f0afc9999d880f
try $WINE "$WINETRICKS_CACHE"/AutoHotkey104706_Install.exe /S 

#fetch openwatcom binary first
download "http://ftp.openwatcom.org/ftp/open-watcom-c-win32-1.8.exe" 44afd1fabfdf0374f614f054824e60ac560f9dc0
#save it to "Program Files"
try cp "$WINETRICKS_CACHE"/open-watcom-c-win32-1.8.exe "$WINEPREFIX"/drive_c/Program\ Files/
#invoke autohotkey script to install it
try $WINE "$WINEPREFIX"/drive_c/Program\ Files/AutoHotKey/AutoHotkey.exe ./install_openwatcom.ahk